package cn.wolfcode.Controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Permission;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.jsonResult;
import cn.wolfcode.service.DepartmentService;
import cn.wolfcode.service.PermissionService;
import cn.wolfcode.service.impl.RoleServiceImpl;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private RoleServiceImpl roleService;

    private DepartmentService departmentService;
    @Autowired
    public void setDepartmentService(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    private PermissionService permissionService;
    @Autowired
    public void setPermissionService(PermissionService permissionService) {
        this.permissionService = permissionService;
    }




//    @RequestMapping("/list")
//    public String listAll(Model model){
//        List<Permission> permissions = permissionMapper.selectAll();
//        model.addAttribute("permissions",permissions);
//        return "permission/list";
//    }

    @RequirePermission(name = "查看权限",experssion = "permission:list")
    @RequestMapping("/list")
    public String listAll(Model model){
        List<Permission> permissions = permissionService.selectAll();
        model.addAttribute("permissions",permissions);
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments", departments);
        return "permission/list";
    }

    @RequirePermission(name = "重新加载",experssion = "permission:rerload")
    @RequestMapping("/reload")
    @ResponseBody
    public jsonResult reload(Model model){
        List<Permission> permissions = permissionService.selectAll();
        model.addAttribute("permissions",permissions);
        jsonResult jsonResult = null;
        try {
            permissionService.rload();
            jsonResult = new jsonResult();
        } catch (Exception e) {
            jsonResult = new jsonResult(false,"加载失败");
            e.printStackTrace();
        }
        return jsonResult;
    }



    @RequirePermission(name = "删除权限",experssion = "permission:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if (id != null){
            permissionService.deleteByPrimaryKey(id);
        }
        return "redirect:/permission/list";
    }

    @RequirePermission(name = "权限新增/编辑",experssion = "permission:saveOrUpdate")
    @RequestMapping("/input")
    public String input(Long id,Model model){
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments", departments);
        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles", roles);
        if (id != null){
            Permission permission = permissionService.selectByPrimaryKey(id);
            model.addAttribute("permission", permission);
        }
        return "permission/input";
    }

    @RequirePermission(name = "权限新增/编辑",experssion = "permission:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Permission permission,Model model, Long[] ids){
        System.out.println(permission);
        if (permission.getId() != null){
            permissionService.updateByPrimaryKey(permission);
            //先删除之前的关联关系在进行重新关联插入
            //这样不需要判断
            //删除关系
            roleService.deleteRelation(permission.getId());
            //添加关系
//            roleService.insertRelation(permission.getId(), ids);
        }else {
            permissionService.insert(permission);
            //由于是是添加,所以不需要删除关系,进行添加关系即可
//            roleService.insertRelation(permission.getId(), ids);
        }
        if (ids != null && ids.length>0){
            roleService.insertRelation(permission.getId(), ids);
        }

        return "redirect:/permission/list";
    }


    //请求跳转到nopermission页面
    @RequestMapping("/nopermission")
    public String nopermission(){
        return "common/nopermission";
    }


}

